package com.foodplus.www.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.foodplus.www.adapter.shop.GoodAdapter;
import com.foodplus.www.api.ExceptionHandle;
import com.foodplus.www.api.ParamManager;
import com.foodplus.www.bean.good.DatasBean;
import com.foodplus.www.bean.good.GoodBean;
import com.foodplus.www.bean.good.GoodsListBean;
import com.foodplus.www.foodplus.R;
import com.foodplus.www.presenter.GoodPresenter;
import com.foodplus.www.view.impl.GoodView;
import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.github.jdsjlzx.recyclerview.LRecyclerViewAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class GoodActivity extends AppCompatActivity implements GoodView {

    private String id;
    @BindView(R.id.good_title_tv)
    TextView title;
    @BindView(R.id.good_back)
    ImageView ivBack;
    @BindView(R.id.good_recycler)
    LRecyclerView recyclerView;
    //适配器
    private GoodAdapter adapter;
    private LRecyclerViewAdapter lRecyclerViewAdapter;

    //容器
    private List<GoodsListBean> list;
    private DatasBean datasBean;

    //p层数据源
    private GoodPresenter presenter;

    //头
    private LinearLayout linearLayout;
    private ImageView iv;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_good);
        ButterKnife.bind(this);
        list = new ArrayList<>();

        //获取item的special_id
        Intent intent = getIntent();
        id = intent.getStringExtra("id");
        Log.e("Good","id:"+id);

        initView();

        //获取数据
        getData();
    }

    private void initView() {
        //RecyclerView
        LinearLayoutManager manager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(manager);
        adapter = new GoodAdapter(list, this);
        setRecycleItemClick();
        //加入query
        lRecyclerViewAdapter = new LRecyclerViewAdapter(adapter);
        recyclerView.setPullRefreshEnabled(false);
        recyclerView.setAdapter(lRecyclerViewAdapter);

        //加头
        addHeader();
    }

    private void setRecycleItemClick() {
        adapter.setOnItemClickListener(new GoodAdapter.OnItemClickListener() {
            @Override
            public void onItemClickListener(int position) {
                Intent intent = new Intent(GoodActivity.this, SipGoodActivity.class);
                intent.putExtra("id", list.get(position-2).getGoods_id());
                GoodActivity.this.startActivity(intent);
                GoodActivity.this.overridePendingTransition(R.anim.shop_trans_enter, 0);
            }
        });
    }

    @OnClick(R.id.good_back)
    public void onClick(View v) {
        getBack();
    }



    private void getData() {
        presenter = new GoodPresenter(this);
        presenter.getGoodsData(ParamManager.getInstance().getGoodParams(id));
    }



    @Override
    public void onSuccess(GoodBean goodBean) {
        list.clear();
        datasBean = null;
        //整理数据
        list.addAll(goodBean.getDatas().getGoods_list());
        datasBean = goodBean.getDatas();
        //载入recycler主体的数据
        adapter.notifyDataSetChanged();
        //载入头的数据
        loadHeaderData();
        //载入标题
        title.setText(datasBean.getSpecial_title());
    }

    @Override
    public void onFail(ExceptionHandle.ResponeThrowable e) {

    }

    @Override
    public void onFinish() {

    }

    private void addHeader() {
        //channel
        linearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.activity_good_header, null);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        linearLayout.setLayoutParams(params);
        iv = (ImageView) linearLayout.findViewById(R.id.good_header_iv);
        tv = (TextView) linearLayout.findViewById(R.id.good_header_tv);
        tv.setText("");
        //加入头
        lRecyclerViewAdapter.addHeaderView(linearLayout);
    }

    private void loadHeaderData() {
        Glide.with(this).load(datasBean.getSpecial_image()).placeholder(R.mipmap.bg_morentu).into(iv);
        tv.setText(datasBean.getSpecial_stitle());
    }

    private void getBack() {
        Log.e("Good", "back");
        GoodActivity.this.overridePendingTransition(0, R.anim.shop_trans_out);
        GoodActivity.this.finish();
    }
    @Override
    public void onBackPressed() {
        super.onBackPressed();
        getBack();
    }
}
